const { xxtUrl } = require("../../../utils/util");
let videoAd = null
// 在页面中定义插屏广告

// pages/sign/sign.js
Page({

  /**
   * 页面的初始数据
   */
  data: { 

    tips: {},
    showtips:false,
    name: '',
    address: '',
    latitude: '',
    longitude: '',
    islogin:0,
    isactivity:0,
    result:'',
    otherid:'',
    user:{
      
    },
    activety:{

    },
    phone:'',
    password:'',
    fileList: [],
    
    array:'',
    aid:'', 
    show: false,
    enc:'',
  },
  bindInput: function(e){
    var that = this;
    var keywords = e.detail.value; 
    // var key = config.Config.key;
    var myAmapFun = new amapFile.AMapWX({key: '3e9bc669522d7ef75ded903eddede824'});
    myAmapFun.getInputtips({
      keywords: keywords,
      location: '',
      success: function(data){
        if(data && data.tips){
          that.setData({
            tips: data.tips,
            showtips:true
          });
        }

      }
    })
  },
  bindSearch: function(e){
    var  that =this
    var myAmapFun = new amapFile.AMapWX({key: '3e9bc669522d7ef75ded903eddede824'});
   myAmapFun.getInputtips({

      keywords: e.target.dataset.keywords,
      location: '',
      success: function(data){
        console.log("data",data)     
        that.setData({
          chosetips:data.tips[0],
          location:data.tips[0].location,
         
          showtips:false
        })
        
      }
    })
    // var url = '../poi/poi?keywords=' + keywords;
    // console.log(url)
    // wx.redirectTo({
    //   url: url
    // })
  },
  getLocation: function () {
   
    var _this = this;
    wx.chooseLocation({
      success: function (res) {
        var name = res.name
        var address = res.address
        var latitude = res.latitude
        var longitude = res.longitude
        _this.setData({
          name: name,
          address: address,
          latitude: latitude,
          longitude: longitude
        })
      },
      complete(r){
        console.log(r)
        console.log(222)
      }
    })
  },

  uvtoken(){
    var that=this
    wx.request({
      url: xxtUrl+'/uvtoken',
      method:'POST',
      data:{
        uf:that.data.user.uf,
        _d:that.data.user._d,
        vc3:that.data.user.vc3,
        uid:that.data.user.UID,
      },
      success(res){
        console.log("uvtoken",res)
        that.setData({
          _token:res.data._token
        })
      }
    })

  },
photo(){
  var that=this
  // 获取vo
  wx.request({
    url: xxtUrl+'/uvtoken',
    method:'POST',
    data:{
      uf:that.data.user.uf,
      _d:that.data.user._d,
      vc3:that.data.user.vc3,
      uid:that.data.user.UID,
    },
    success(res){
      console.log("uvtoken",res)
      that.setData({
        _token:res.data._token
      })
    }
  })
  wx.chooseImage({
    count: 0,
  })

  wx.request({
    url: xxtUrl+'/photo',
    method:'POST',
    data:{
      uf:that.data.user.uf,
      _d:that.data.user._d,
      vc3:that.data.user.vc3,
      uid:that.data.user.UID,
      name:that.data.user.name,
      aid:that.data.aid,
      fid:that.data.fid,
      objectId:'aaaaaaaaaaaaaaaa'
    },
    success(res){
        console.log("photot",res)
        if(res.data=="success"){
          wx.showToast({
            title: '签到成功',
            icon:'success'
          })
        }else{
          wx.showToast({
            title: '签到失败',
            icon:'error'
          })
        }
    }
  })
},

  upload(){
    var that=this
    wx.chooseImage({
      count: that.data.count, // 默认3
      sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ["album", "camera"], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        console.log("wxchooseimge",res)
        var tempFilePaths = res.tempFilePaths
        for (var i = 0; i < tempFilePaths.length; i++) {
          // uf:that.data.user.uf,
          // _d:that.data.user._d,
          // vc3:that.data.user.vc3,
          // uid:that.data.user.UID,
          // name:that.data.user.name,
          // aid:that.data.aid,
          // fid:that.data.fid,

          // let data= new FormData()
          // data.append('uf',that.data.user.uf)
          // data.append('_d',that.data.user._d)
          // data.append('_uid',that.data.user.uid)
          // data.append('vc3',that.data.user.vc3)
          // data.append('file',i)
          wx.uploadFile({
            url: xxtUrl+'/upload?_token='+that.data._token,
            filePath:tempFilePaths[i],
            name:'file',
            data:{
               uf:that.data.user.uf,
                _d:that.data.user._d,
                vc3:that.data.user.vc3,
                uid:that.data.user.UID,
                // file:tempFilePaths[i]

              // _token:'c31f4714cd8dce82cedd018b0502c004'
            },
            formData: {
              'user': 'test'
              },
            header: {
              "content-type": "application/x-www-form-urlencoded"
            },
            success: function (res) {
              console.log("upload",res)
              if (res.statusCode == 200) {
                wx.showToast({
                  title: "上传成功",
                  icon: "none",
                  duration: 1500
                })
   
                that.data.imgs.push(JSON.parse(res.data).data)
   
                that.setData({
                  imgs: that.data.imgs
                })
              }
            },
            fail: function (err) {
              wx.showToast({
                title: "上传失败",
                icon: "none",
                duration: 2000
              })
            },
            complete: function (result) {
              console.log(result.errMsg)
            }
          })
        }
      }
    })


  },

  shoushiqiandao(){
    var that=this
    wx.request({
      url: xxtUrl+'/general',
      method:'POST',
      data:{
           uf:that.data.user.uf,
          _d:that.data.user._d,
          vc3:that.data.user.vc3,
          uid:that.data.user.UID,
          name:that.data.user.name,
          aid:that.data.aid,
          fid:that.data.fid,
      },
      success(res){
        wx.showToast({
          title: res.data,
        })
        console.log(res)
      }
    })

  },
  // kongbaiweizhiqiandao(){
  //   var that =this
  //   var longitude=this.data.location.split(',')[0]
  //   var latitude=this.data.location.split(',')[1];

  //   console.log("latitude",latitude)
  //   console.log("longitude",longitude)
  //   wx.request({
  //     url: xxtUrl+'/location',
  //     method:'POST',
  //     data:{
  //          uf:that.data.user.uf,
  //         _d:that.data.user._d,
  //         vc3:that.data.user.vc3,
  //         uid:that.data.user.UID,
  //         name:that.data.user.name,
  //         aid:that.data.aid,
  //         fid:that.data.fid,
  //         address:that.data.chosetips.name,
  //         lat: that.data.location.split(',')[0] , //'23.437261', 
  //         lon: that.data.location.split(',')[1] //'113.178439'
  //     },
  //     success(res){
  //       wx.showToast({
  //         title: res.data,
  //       })
  //       console.log(res)
  //     }
  //   })
  // },
  weizhiqiandao(){
    var that=this
    var lat = that.data.latitude+0.006277;
    var lon = that.data.longitude+0.006621
    wx.request({
      url: xxtUrl+'/location',
      method:'POST',
      data:{
           uf:that.data.user.uf,
          _d:that.data.user._d,
          vc3:that.data.user.vc3,
          uid:that.data.user.UID,
          name:that.data.user.name,
          aid:that.data.aid,
          fid:that.data.fid,
          address:that.data.address,
          lat: lat,//'23.437261', 
          lon: lon  //'113.178439'
      },
      success(res){
        wx.showToast({
          title: res.data,
        })
        console.log(res)
      }
    })
  },



  afterRead(event) {



    // const { file } = event.detail;
    // // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    // wx.uploadFile({
    //   url: 'https://example.weixin.qq.com/upload', // 仅为示例，非真实的接口地址
    //   filePath: file.url,
    //   name: 'file',
    //   formData: { user: 'test' },
    //   success(res) {
    //     // 上传完成需要更新 fileList
    //     const { fileList = [] } = this.data;
    //     fileList.push({ ...file, url: res.data });
    //     this.setData({ fileList });
    //   },
    // });
  },
  getQueryString:function (url, name) {  
    var reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i');  
    var r = url.substr(1).match(reg);  
    if (r != null) {      
    return r[2];  
    }  
    return null;
    },


  scanCode:function(){
    var that=this;
    wx.scanCode({
      success(res){
        console.log(res)
        var enc=that.getQueryString(res.result,'enc')
        that.setData({
          enc:enc
          // enc:'DB6C27A013A1BA9232CC14CE41351BD0'
        })
        console.log(res.result)
        console.log('---that.data.enc',that.data.enc)
      }
    })
     
  },
   getlogin:function(){
    // console.log("0-----------------")
    var that =this
    wx.request({
      url: xxtUrl+'/login',
      method:'POST',
      data:{
        phone:this.data.phone,
        password:this.data.password
      },
      success(res){
       console.log("res",res)
       console.log("resdata.data",res.data)
        that.setData({
        user:res.data,
        islogin:1,
       })
       console.log("---",that.data.user)
      },
      error(e){
        console.log("ee",e)
      }
    })
  
  },
  getActivety:function(){
    var that =this
    wx.request({
      url: xxtUrl+'/activity',
      method:'POST',
      data:{
        uf:that.data.user.uf,
        _d:that.data.user._d,
        vc3:that.data.user.vc3,
        uid:that.data.user.UID,
      },
      success(res){
        console.log("avtivetyres"+JSON.stringify(res))
        that.setData({
          activety:res,
          isactivity:1
        })
        console.log("activety",that.data.activety)
      }
    })
  },

 
  signUP:function(){
    var that =this;
      if (that.data.enc==='') {
        wx.showToast({
          title: '拍照内容为空',
          icon:'error'
        })
        return
      }
   
      wx.request({
        url: xxtUrl+'/qrcode',
        method:'POST',
        data:{
          uf:that.data.user.uf,
          _d:that.data.user._d,
          vc3:that.data.user.vc3,
          uid:that.data.user.UID,
          name:that.data.user.name,
          aid:that.data.aid,
          fid:that.data.fid,
          enc:that.data.enc
        },
        success(res){
         that.setData({
            result:res.data
          })
          wx.showToast({
            title: res.data,
          })
          console.log("res",res)
        }
      })
  },
  showPopup() {
    this.setData({ show: true });
  },

  onClose() {
    this.setData({ show: false });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this; 

   console.log(options)
      this.setData({
        aid:options.code,
        otherid:options.otherid,
        user:wx.getStorageSync('signup')
      })
    console.log("signup",this.data.user)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // loginAgain()
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.record']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})